import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

/**
 * @author: CoderXSLee
 * @github: https://github.com/coderxslee
 * @version: 1.0.0
 * @copyright: Copyright © 2023-2024
 * @license: MIT License
 * @date: 2024-09-4
 * @description: Loading
 */

/// 用于全局显示隐藏Loading Widget
class Loading {
  // 初始化Loading样式
  static void init() {
    SmartDialog.config.toast = SmartConfigToast(
      alignment: Alignment.center, // 设置Toast显示的位置
      animationType: SmartAnimationType.fade, // 设置动画类型
      maskColor: Colors.black.withOpacity(0.6), // 遮罩颜色
      displayTime: const Duration(seconds: 2), // 显示时间
    );

    // SmartDialog.config.loading = SmartConfigLoading(
    //   maskColor: Colors.black.withOpacity(0.6), // 遮罩颜色
    //   leastLoadingTime: const Duration(milliseconds: 300),
    //   animationTime: const Duration(milliseconds: 200),
    // );
  }


  // 显示加载中
  static void showLoading(String text) {
    SmartDialog.showLoading(msg: text, displayTime: const Duration(seconds: 60));
  }

  // 关闭当前的加载中...
  static void dismissLoading() async {
    await SmartDialog.dismiss(status: SmartStatus.loading);
  }
  // 显示加载中 和 进度
  static double _progress = 0.0;
  static void showProgress(String message, int send, int total) {
    if (total <= 0) return;
    _progress = send / total;
    // 显示或更新对话框
    SmartDialog.showNotify(
        msg: message, // 这里是要显示的消息
        notifyType: NotifyType.alert, // 这里是通知类型
        tag: 'progress',
        builder: (_) => Container(
          padding: const EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12),
          ),
          width: 300,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(message, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
              const SizedBox(height: 20),
              LinearProgressIndicator(
                value: _progress,
                minHeight: 8,
                backgroundColor: Colors.grey[300],
                valueColor: const AlwaysStoppedAnimation<Color>(Colors.blue),
              ),
              const SizedBox(height: 10),
              Text('${(_progress * 100).toStringAsFixed(0)}%', style: const TextStyle(fontSize: 16)),
            ],
          ),
        )
    );
    // 如果进度达到100%，关闭对话框
    if (_progress >= 1.0) SmartDialog.dismiss(tag: 'progress');
  }

  // 显示信息
  static void showMessageToast(String message) {
    double charDisplayTime = 0.15;
    int displayTime = (message.length * charDisplayTime).ceil();
    displayTime = displayTime.clamp(1, 5); // 限制展示时间最小1秒，最大为5秒钟
    SmartDialog.showToast(
      message,
      alignment: const Alignment(0.0, -0.25),
      displayType: SmartToastType.onlyRefresh,
      displayTime: Duration(seconds: displayTime),
      animationType: SmartAnimationType.centerFade_otherSlide,
    );
  }


  // 显示成功信息
  static void showSuccessToast(String message) {
    SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.success,
      alignment: const Alignment(0.0, -0.25),
      animationType: SmartAnimationType.centerFade_otherSlide,
    );
  }

  // 显示成功信息
  static void showFailureToast(String message) {
    SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.failure,
      alignment: const Alignment(0.0, -0.25),
      animationType: SmartAnimationType.centerFade_otherSlide,
    );
  }



  // 显示错误信息
  static void showErrorToast(String message) {
    double charDisplayTime = 0.2;
    int displayTime = (message.length * charDisplayTime).ceil();
    displayTime = displayTime.clamp(1, 5); // 限制展示时间最小1秒，最大为5秒钟
    SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.error,
      alignment: const Alignment(0.0, -0.25),
      displayTime: Duration(seconds: displayTime),
      animationType: SmartAnimationType.centerScale_otherSlide,
    );
  }

  // 显示注意信息
  static void showAlertToast(String message) {
    double charDisplayTime = 0.2;
    int displayTime = (message.length * charDisplayTime).ceil();
    displayTime = displayTime.clamp(1, 5); // 限制展示时间最小1秒，最大为5秒钟
    SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.alert,
      alignment: const Alignment(0.0, -0.25),
      displayTime: Duration(seconds: displayTime),
      animationType: SmartAnimationType.centerFade_otherSlide,
    );
  }

}
